/**
 * <copyright>
 * </copyright>
 *
 * 
 */
package de.claaswilke.presents.resource.presents.mopp;

/**
 * The PresentsTokenResolverFactory class provides access to all generated token
 * resolvers. By giving the name of a defined token, the corresponding resolve can
 * be obtained. Despite the fact that this class is called TokenResolverFactory is
 * does NOT create new token resolvers whenever a client calls methods to obtain a
 * resolver. Rather, this class maintains a map of all resolvers and creates each
 * resolver at most once.
 */
public class PresentsTokenResolverFactory implements de.claaswilke.presents.resource.presents.IPresentsTokenResolverFactory {
	
	private java.util.Map<String, de.claaswilke.presents.resource.presents.IPresentsTokenResolver> tokenName2TokenResolver;
	private java.util.Map<String, de.claaswilke.presents.resource.presents.IPresentsTokenResolver> featureName2CollectInTokenResolver;
	private static de.claaswilke.presents.resource.presents.IPresentsTokenResolver defaultResolver = new de.claaswilke.presents.resource.presents.analysis.PresentsDefaultTokenResolver();
	
	public PresentsTokenResolverFactory() {
		tokenName2TokenResolver = new java.util.LinkedHashMap<String, de.claaswilke.presents.resource.presents.IPresentsTokenResolver>();
		featureName2CollectInTokenResolver = new java.util.LinkedHashMap<String, de.claaswilke.presents.resource.presents.IPresentsTokenResolver>();
		registerTokenResolver("NUMBER", new de.claaswilke.presents.resource.presents.analysis.PresentsNUMBERTokenResolver());
		registerTokenResolver("TEXT", new de.claaswilke.presents.resource.presents.analysis.PresentsTEXTTokenResolver());
	}
	
	public de.claaswilke.presents.resource.presents.IPresentsTokenResolver createTokenResolver(String tokenName) {
		return internalCreateResolver(tokenName2TokenResolver, tokenName);
	}
	
	public de.claaswilke.presents.resource.presents.IPresentsTokenResolver createCollectInTokenResolver(String featureName) {
		return internalCreateResolver(featureName2CollectInTokenResolver, featureName);
	}
	
	protected boolean registerTokenResolver(String tokenName, de.claaswilke.presents.resource.presents.IPresentsTokenResolver resolver){
		return internalRegisterTokenResolver(tokenName2TokenResolver, tokenName, resolver);
	}
	
	protected boolean registerCollectInTokenResolver(String featureName, de.claaswilke.presents.resource.presents.IPresentsTokenResolver resolver){
		return internalRegisterTokenResolver(featureName2CollectInTokenResolver, featureName, resolver);
	}
	
	protected de.claaswilke.presents.resource.presents.IPresentsTokenResolver deRegisterTokenResolver(String tokenName){
		return tokenName2TokenResolver.remove(tokenName);
	}
	
	private de.claaswilke.presents.resource.presents.IPresentsTokenResolver internalCreateResolver(java.util.Map<String, de.claaswilke.presents.resource.presents.IPresentsTokenResolver> resolverMap, String key) {
		if (resolverMap.containsKey(key)){
			return resolverMap.get(key);
		} else {
			return defaultResolver;
		}
	}
	
	private boolean internalRegisterTokenResolver(java.util.Map<String, de.claaswilke.presents.resource.presents.IPresentsTokenResolver> resolverMap, String key, de.claaswilke.presents.resource.presents.IPresentsTokenResolver resolver) {
		if (!resolverMap.containsKey(key)) {
			resolverMap.put(key,resolver);
			return true;
		}
		return false;
	}
	
}
